package cn.fanqinghong.ymcc.service.impl;

import cn.fanqinghong.ymcc.domain.Login;
import cn.fanqinghong.ymcc.dto.RefreshDto;
import cn.fanqinghong.ymcc.mapper.LoginMapper;
import cn.fanqinghong.ymcc.result.JSONResult;
import cn.fanqinghong.ymcc.service.ILoginService;
import cn.fanqinghong.ymcc.util.AssertUtil;
import cn.fanqinghong.ymcc.util.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * <p>
 * 登录表 服务实现类
 * </p>
 *
 * @author superman.fan
 * @since 2022-07-15
 */
@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements ILoginService {

    public final String TOKEN_URL = "http://localhost:10040/oauth/token?client_id=%s&client_secret=%s&grant_type=password&redirect_uri=http://www.baidu.com&username=%s&password=%s";
    public final String REFRESH_TOKEN_URL =  "http://localhost:10040/oauth/token?grant_type=refresh_token&refresh_token=%s&client_id=%s&client_secret=%s";
    @Override
    public JSONResult common(Login login) {
        //1.判断账号密码存在
        Wrapper<Login> wrapper = new EntityWrapper<>();
        wrapper.eq("username", login.getUsername());
        Login loginTmp = selectOne(wrapper);
        AssertUtil.isNotNull(loginTmp,"账户不存在");
        //2.拼接请求
        String clientId = "website";
        String clientSecret = "123";
        if (login.getType().intValue() == 0){
            clientId = "admin";
            clientSecret = "123";
        }
        String url = String.format(TOKEN_URL, clientId, clientSecret, login.getUsername(), login.getPassword());
        String resultStr = HttpUtil.sendPost(url, null);
        String resultStrTpm = new String(resultStr.getBytes(), StandardCharsets.UTF_8);

        Map<String,Object> resultMap = JSONObject.parseObject(resultStrTpm, Map.class);
        //返回token给前端
        if (resultMap.containsKey("error_description")){
            return JSONResult.error(resultMap.get("error_description").toString());
        }else {
            Integer expires_in = (Integer)resultMap.get("expires_in");//多少毫秒后过期
            resultMap.put("expiresTime",System.currentTimeMillis() + expires_in);
            resultMap.put("loginId",loginTmp.getId());
            return JSONResult.success(resultMap);
        }

    }


    /**
     * 刷新过期时间
     * @param dto
     * @return
     */
    @Override
    public JSONResult refresh(RefreshDto dto) {
        //1.判断账号密码存在
        Wrapper<Login> warpper = new EntityWrapper<>();
        warpper.eq("username",dto.getUsername());
        Login login = selectOne(warpper);
        AssertUtil.isNotNull(login,"账户不存在");
        //2.拼接请求
        String clientId = "website";
        String clientSecret = "123";
        if (login.getType().intValue() == 0){
            clientId = "admin";
            clientSecret = "123";
        }
        String url = String.format(REFRESH_TOKEN_URL, dto.getRefreshToken(), clientId,clientSecret);
        String resultStr = HttpUtil.sendPost(url, null);
        String resultStrTpm = new String(resultStr.getBytes(), StandardCharsets.UTF_8);

        Map<String,Object> resultMap = JSONObject.parseObject(resultStrTpm, Map.class);
        //返回token给前端
        if (resultMap.containsKey("error_description")){
            return JSONResult.error(resultMap.get("error_description").toString());
        }else {
            Integer expires_in = (Integer)resultMap.get("expires_in");//多少毫秒后过期
            resultMap.put("expiresTime",System.currentTimeMillis() + expires_in);
            resultMap.put("loginId",login.getId());
            return JSONResult.success(resultMap);
        }
    }
}
